package com.yami.shop.personnft.common.listener;

import cn.hutool.core.util.StrUtil;
import com.yami.shop.bean.event.BusinessAddAirdropPersonnftEvent;
import com.yami.shop.bean.event.BusinessAddBuyPersonnftEvent;
import com.yami.shop.bean.event.BusinessNftResultFailEvent;
import com.yami.shop.bean.event.BusinessNftResultSuccessEvent;
import com.yami.shop.bean.model.BusinessNft;
import com.yami.shop.bean.personnft.AddPersonnft;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.personnft.common.service.PersonNftService;
import com.yami.shop.service.BusinessNftRecordService;
import com.yami.shop.service.BusinessNftService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 确认订单信息时的默认操作
 *
 * @author LGH
 */
@Slf4j
@Component("addAirdropPersonnftListener")
@AllArgsConstructor
public class AddAirdropPersonnftListener {

    @Autowired
    private BusinessNftService businessNftService;

    @Autowired
    private BusinessNftRecordService businessNftRecordService;

    @Autowired
    private PersonNftService personNftService;

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 空投
     */
    @EventListener(BusinessAddAirdropPersonnftEvent.class)
    @Order(AddPersonnft.AIRDROOP)
    @Async("lazyTraceExecutor")
    public void defaultBusinessAddPersonnListener(BusinessAddAirdropPersonnftEvent event) {
        log.info("*********************************************************空投持仓增加取币记录开始");
        BusinessNft businessNft = event.getBusinessNft();
        if(RedisUtil.hasKey("businessNftTaskBusinessId::"+businessNft.getBusinessNftId())){
            RedisUtil.del("businessNftTaskBusinessId::"+businessNft.getBusinessNftId());
        }else{
            log.info("*********************************************************空投持仓增加取币记录重复退出");
            return;
        }
        try {
            personNftService.addPersonnftByBuy(businessNft);
            businessNftService.updateStatus(businessNft.getBusinessNftId(),4,null);
            businessNftRecordService.updateStatusByBusinessNftId(businessNft.getBusinessNftId(),2,null);
            //回调
            applicationContext.publishEvent(new BusinessNftResultSuccessEvent(businessNft.getBusinessNftId(),businessNft.getOutId()));
        }catch (Exception e){
            log.info("错误信息",e);
            e.printStackTrace();
            //回调
            businessNftRecordService.updateStatusByBusinessNftId(businessNft.getBusinessNftId(),1,getString(e));
            businessNftService.updateStatus(businessNft.getBusinessNftId(),5,getString(e));
            applicationContext.publishEvent(new BusinessNftResultFailEvent(businessNft.getBusinessNftId(),businessNft.getOutId(),getString(e)));
        }

        log.info("*********************************************************空投持仓增加取币记录结束");
    }

    private String getString(Exception e) {
        String msg = "";
        if(StrUtil.isEmpty(e.getMessage())&& Arrays.toString(e.getStackTrace()).length()<=490){
            msg = "null--"+ Arrays.toString(e.getStackTrace()).substring(0);
        }else if(StrUtil.isEmpty(e.getMessage())&&Arrays.toString(e.getStackTrace()).length()>490){
            msg = "null--"+ Arrays.toString(e.getStackTrace()).substring(0,490);
        }else if(e.getMessage().length()>500){
            msg = e.getMessage().substring(0,500);
        }else{
            msg = e.getMessage().substring(0);
        }
        return msg;
    }
}
